package com.itheima.controller;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    //添加
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门");
        deptService.addDept(dept);
        return Result.success();
    }

    //修改
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("修改部门");
        deptService.updateDept(dept);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门");
        deptService.deleteDept(id);

        return Result.success();
    }

    //查询
    @GetMapping
    public Result findAllDept() {
        log.info("查找全部部部门信息");
        List<Dept> allDept = deptService.findAllDept();
        return Result.success(allDept);
    }

    //id查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Dept byId = deptService.findById(id);
        return Result.success(byId);
    }

}
